//
//  TBCRootViewController.m
//  待定
//
//  Created by BoyaLee on 15/11/10.
//  Copyright © 2015年 BoyaLee. All rights reserved.
//

#import "TBCRootViewController.h"
#import "AppDelegate.h"

@interface TBCRootViewController () <UIScrollViewDelegate>

@property (strong, nonatomic) UIScrollView *scrollViewLaunch;
@property (strong, nonatomic) UIPageControl *pageControlLaunch;

@end

@implementation TBCRootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self initRootViews];
    
}

- (void)initRootViews {
    
    NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentPath = [array lastObject];
    NSString *tbcTxt = [documentPath stringByAppendingPathComponent:@"tbc.txt"];
    
    NSString *content = @"存在此文件";
    
    BOOL result = [content writeToFile:tbcTxt atomically:YES encoding:NSUTF8StringEncoding error:nil];
    if (result) {
        
        NSLog(@"写入成功");
        
    }else {
        
        NSLog(@"写入失败");
        
    }
    
    [self initLaunchViews];
    
}

#pragma mark -- 滚动图片和PageControl
- (void)initLaunchViews {
    
    NSArray *imgArray = [NSArray arrayWithObjects:@"ScrollViewLaunchImage1@2x",@"ScrollViewLaunchImage2@2x",@"ScrollViewLaunchImage3@2x",@"ScrollViewLaunchImage4@2x",@"ScrollViewLaunchImage5@2x",@"ScrollViewLaunchImage6@2x", nil];
    self.scrollViewLaunch = [[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.scrollViewLaunch.contentSize = CGSizeMake(self.view.bounds.size.width * imgArray.count, self.view.bounds.size.height);
    self.scrollViewLaunch.pagingEnabled = YES;
    self.scrollViewLaunch.showsHorizontalScrollIndicator = NO;
    self.scrollViewLaunch.showsVerticalScrollIndicator = NO;
    self.scrollViewLaunch.delegate = self;
    
    [NSTimer scheduledTimerWithTimeInterval:0 target:self selector:@selector(launchScroll:) userInfo:nil repeats:YES];
    
    [self.view addSubview:self.scrollViewLaunch];
    for(int i = 0; i < imgArray.count; i++) {
        
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i * self.view.bounds.size.width, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
        imageView.image = [UIImage imageNamed:imgArray[i]];
        [self.scrollViewLaunch addSubview:imageView];
        
    }
    
    self.pageControlLaunch = [[UIPageControl alloc] initWithFrame:CGRectMake(WIDTH / 2 - 100, HEIGHT - 67, 200, 40)];
    self.pageControlLaunch.numberOfPages = imgArray.count;
    self.pageControlLaunch.currentPage = 0;
    self.pageControlLaunch.pageIndicatorTintColor = [UIColor whiteColor];
    self.pageControlLaunch.currentPageIndicatorTintColor = [UIColor redColor];
    [self.view addSubview:self.pageControlLaunch];
    
}

- (void)launchScroll:(NSTimer *)timer {
    
    [UIView animateWithDuration:2 animations:^ {
        
        self.scrollViewLaunch.alpha = 1;
        
    } completion:^(BOOL finished) {
        
        [timer invalidate];
        
    }];
    
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
#pragma mark -- 当图片全部滑完后 又向后多滑了10 的时候就做下一个动作
    self.pageControlLaunch.currentPage = self.scrollViewLaunch.contentOffset.x / WIDTH;
    if(self.scrollViewLaunch.contentOffset.x + WIDTH - self.scrollViewLaunch.contentSize.width > 30) {
        
        AppDelegate *app = [UIApplication sharedApplication].delegate;
        [app initRootViewControllers];
        
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
